from peewee import AutoField, CharField, BigIntegerField, BigAutoField, BooleanField, SmallIntegerField
from playhouse.mysql_ext import JSONField
from common.model.abstrct import AbstractModel, IntEnumField, AbstractModelWithID
from enums.design_page_enum import DesignPageContentTypeEnum


class WebSite(AbstractModel):
    class Meta:
        table_name = "tb_tenant_website"
        indexes = (
            (("tenant_id", ), False),
        )
    tenant_id = BigIntegerField(help_text="租户id")
    website_id = BigAutoField(help_text="网站id")
    website_name = CharField(max_length=16, help_text="网站名称")


class Page(AbstractModel):

    class Meta:
        table_name = "tb_tenant_page"
        indexes = (
            (("tenant_id",), False),
        )

    tenant_id = BigIntegerField(help_text="租户id")
    page_id = BigAutoField(primary_key=True, help_text="页面id")
    page_name = CharField(max_length=16, help_text="页面名称")
    is_default = BooleanField(help_text="是否是默认页面，也就是主页")


# 一个页面可以由多个组件组成
class MiddlePageComponent(AbstractModelWithID):
    class Meta:
        table_name = "tb_middle_page_component"
        indexes = (
            (("tenant_id", "page_id"), False),
            (("page_id", ), False),
            (("component_code", ), False)  # 统计组件有多少人在用
        )
    tenant_id = BigIntegerField(help_text="租户id")
    page_id = BigIntegerField(help_text="页面id")
    component_code = CharField(help_text="组件code")
    priority = SmallIntegerField(help_text="优先级,页面内的组件排序问题")
    # show_content 这个不需要数据库校验
    show_content = JSONField(help_text="展示的内容信息")

    component_style = JSONField(help_text="组件样式")


# 一个页面中的组件可以绑定多个内容信息，比如活动 商品， 广告之类的
# 这个是需要数据库校验是否存在的
class MiddlePageComponentContent(AbstractModelWithID):
    class Meta:
        table_name = "tb_middle_page_component_content"
        indexes = (
            (("page_component_id", ), False),
        )
    page_component_id = BigIntegerField(help_text="页面组件中间表的id")
    content_type = IntEnumField(help_text="页面组件中绑定的内容类型 比如是商品还是活动还是什么", choices=DesignPageContentTypeEnum)
    content_id = BigIntegerField(help_text="内容id 是商品就是商品id， 是活动就是活动id")
    priority = SmallIntegerField(help_text="优先级,可能一个页面的一个组件里面有多个内容")


# 一个网站可以有多个页面
class MiddleWebSitePage(AbstractModelWithID):
    class Meta:
        table_name = "tb_middle_website_page"
        indexes = (
            (("website_id", ), False),
            (("page_id", ), False)
        )
    website_id = BigIntegerField(help_text="网站id")
    page_id = BigIntegerField(help_text="页面id")
